﻿// --- Copyright (c) 2010 Steven Hartgers ---
// Copyright (C) 2010 Steven Hartgers All rights are reserved.
// Author contact: fiinix00/at\gmail.com
// ------------------------------------------------------------------------
// This program is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// This program is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more detail
//  
// You should have received a copy of the GNU General Public License 
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;

namespace Hack.ImportersExporters.Exporters
{
    /// <summary>
    /// Author: Armando
    /// A simple method to write data in a DataTable or DataView format in C# to a CSV file.
    /// Platform: .NET Framework 2.0
    /// </summary>
    public static class DataViewToCSV
    {
        public static void ExportDatatviewToCsv(string iFilename, DataView dv)
        {
            // Open output stream
            StreamWriter swFile = new StreamWriter(iFilename);

            // Header
            string[] colLbls = new string[dv.Table.Columns.Count];
            for (int i = 0; i < dv.Table.Columns.Count; i++)
            {
                colLbls[i] = dv.Table.Columns[i].ColumnName;
                colLbls[i] = GetWriteableValueForCsv(colLbls[i]);
            }

            // Write labels
            swFile.WriteLine(string.Join(",", colLbls));

            // Rows of Data
            foreach (DataRowView rowData in dv)
            {
                string[] colData = new string[dv.Table.Columns.Count];
                for (int i = 0; i < dv.Table.Columns.Count; i++)
                {
                    object obj = rowData[i];
                    colData[i] = GetWriteableValueForCsv(obj);
                }

                // Write data in row
                swFile.WriteLine(string.Join(",", colData));
            }

            // Close output stream
            swFile.Close();
        }

        public static void ExportDatatableToCsv(string iFilename, DataTable dt)
        {
            // Open output stream
            StreamWriter swFile = new StreamWriter(iFilename);

            // Header
            string[] colLbls = new string[dt.Columns.Count];

            for (int i = 0; i < dt.Columns.Count; i++)
            {
                colLbls[i] = dt.Columns[i].ColumnName;
                colLbls[i] = GetWriteableValueForCsv(colLbls[i]);
            }

            // Write labels
            swFile.WriteLine(string.Join(",", colLbls));

            // Rows of Data
            foreach (DataRow rowData in dt.Rows)
            {
                string[] colData = new string[dt.Columns.Count];

                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    object obj = rowData[i];
                    colData[i] = GetWriteableValueForCsv(obj);
                }

                // Write data in row
                swFile.WriteLine(string.Join(",", colData));
            }

            // Close output stream
            swFile.Close();
        }

        internal static string GetWriteableValueForCsv(object obj)
        {
            // Nullable types to blank
            if (obj == null || obj == Convert.DBNull)
                return "";

            // if string has no ','
            if (obj.ToString().IndexOf(",") == -1)
                return obj.ToString();

            // remove backslahes
            return "\"" + obj.ToString() + "\"";
        }
    }
}
